package com.asd.system.controller;

import com.asd.common.Result;
import com.asd.system.entity.Images;
import com.asd.system.service.IImagesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author
 * @createtime 2021-09-27 9:27
 * @description
 */

@Api(tags = "图片上传api")
@RestController
@RequestMapping("/api")
public class FileController {


    @Autowired
    private IImagesService imagesService;


//    final private String systemPath = "http://192.168.124.12:8080/images/";//本地回显地址前缀
//    final private String imagePathRoot = "E:\\upload\\images\\";//存放图片的本地路径

    final private String systemPath = "http://dtxt.iansn.com/pictures/";//服务器回显地址前缀
    final private String imagePathRoot = "/www/wwwroot/dtxt.iansn.com/pictures/";//存放图片的服务器路径



    @ApiOperation(value = "批量图片上传", notes = "批量图片上传")
    @PostMapping("/filelist")
    public Result<?> uploadFileList(@RequestParam("fileimageList") List<MultipartFile> fileimageList) throws IOException {

//        final String imagePathRoot = "E:\\upload\\images\\";//存放图片的本地路径
//        final String imagePathRoot = "/www/wwwroot/dtxt.iansn.com/pictures/";//存放图片的服务器路径


        List<Images> list = new ArrayList<>();

        if (!fileimageList.isEmpty()) {
            for (int i = 0; i < fileimageList.size(); i++) {

                MultipartFile fileimage = fileimageList.get(i);

                File file = new File(imagePathRoot);
                if (!file.exists()) {
                    file.mkdirs();
                }

                if (fileimage.isEmpty()) {
                    return Result.error("请选择文件！！！");
                }
                //获取原文件名
                String fileName = fileimage.getOriginalFilename();
                //获取后缀名
                String fileType = fileName.substring(fileName.lastIndexOf("."));
                //获取UUID为新的图片进行重新赋值（防止重复）
                String uuid = UUID.randomUUID().toString().replace("-", "");
                String imageFilePath = imagePathRoot + uuid + fileType;
                fileimage.transferTo(new File(imageFilePath));


                String echoPath = systemPath + uuid + fileType;

                String path = imageFilePath;

                Images images = new Images();


                images.setName(fileName);
                images.setPath(path);

                images.setViceEchoPath(echoPath);
                imagesService.save(images);

                list.add(images);

            }

        }

        return Result.ok(list);
//        return Result.ok("图片上传失败");

    }


    @ApiOperation(value = "图片上传", notes = "图片上传")
    @PostMapping("/file")
    public Result<?> uploadFile(@RequestParam("fileimage") MultipartFile fileimage) throws IOException {

//        final String imagePathRoot = "E:\\upload\\images\\";//存放图片的本地路径
//        final String imagePathRoot = "/www/wwwroot/dtxt.iansn.com/pictures/";//存放图片的服务器路径

        File file = new File(imagePathRoot);
        if (!file.exists()) {
            file.mkdirs();
        }

        if (fileimage.isEmpty()) {
            return Result.error("请选择文件！！！");
        }
        //获取原文件名
        String fileName = fileimage.getOriginalFilename();
        //获取后缀名
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        //获取UUID为新的图片进行重新赋值（防止重复）
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String imageFilePath = imagePathRoot + uuid + fileType;
        fileimage.transferTo(new File(imageFilePath));


        String echoPath = systemPath + uuid + fileType;


        String path = imageFilePath;

        Images images = new Images();

        images.setName(fileName);
        images.setPath(path);

        images.setPrimaryEchoPath(echoPath);
        imagesService.save(images);

        return Result.ok(images);
    }

    @ApiOperation(value = "主图片图片删除", notes = "主图片图片删除")
    @DeleteMapping("/deletePrimary")
    public Result<?> deletePrimaryImage(@RequestParam("primaryEchoPath") String primaryEchoPath) {

//        final String imagePathRoot = "E:\\upload\\images\\";//存放图片的本地路径
//        final String imagePathRoot = "/www/wwwroot/dtxt.iansn.com/pictures/";//存放图片的服务器路径

        //获取服务器中存储的图片名称
        String fiName = primaryEchoPath.substring(primaryEchoPath.lastIndexOf("/"));

        File file = new File(imagePathRoot + fiName);
        try {
            boolean flag = file.delete(); // 删除照片
            imagesService.deletByPrimaryEchoPath(primaryEchoPath);

            if (flag) {
                return Result.ok("删除成功");
            } else {
                return Result.ok("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ok("删除失败");
        }

    }

    @ApiOperation(value = "副图片图片删除", notes = "副图片图片删除")
    @DeleteMapping("/deleteVice")
    public Result<?> deleteViceImage(@RequestParam("viceEchoPath") String viceEchoPath) {

//        final String imagePathRoot = "E:\\upload\\images\\";//存放图片的本地路径
//        final String imagePathRoot = "/www/wwwroot/dtxt.iansn.com/pictures/";//存放图片的服务器路径

        //获取服务器中存储的图片名称
        String fiName = viceEchoPath.substring(viceEchoPath.lastIndexOf("/"));

        File file = new File(imagePathRoot + fiName);
        try {
            boolean flag = file.delete(); // 删除照片
            imagesService.deletByViceEchoPath(viceEchoPath);

            if (flag) {
                return Result.ok("删除成功");
            } else {
                return Result.ok("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ok("删除失败");
        }
    }
}
